import { ElMessage } from 'element-plus'
import router from '@/router'
import { useUserStore } from '@/stores/user'

export const handleApiError = (error) => {
  const userStore = useUserStore()
  
  if (error.response) {
    switch (error.response.status) {
      case 401:
        userStore.clearUserInfo()
        router.push('/')
        ElMessage.error('登录已过期，请重新登录')
        break
      case 403:
        ElMessage.error('没有权限执行此操作')
        break
      case 404:
        ElMessage.error('请求的资源不存在')
        break
      default:
        ElMessage.error(error.response.data?.message || '操作失败')
    }
  } else {
    ElMessage.error('网络错误，请稍后重试')
  }
} 